unit uThreadSMART;

interface

uses
  Winapi.Windows, System.SysUtils, System.Classes, Vcl.Forms, Vcl.ComCtrls;

type
{$M+}
  TThreadSMART = class
  private
  published
    procedure InitSMART;
    procedure SmartEnalbe;
    procedure SmartDisenalbe;
    procedure SmartEnableAutoSave;
    procedure SmartDisenalbeAutoSave;
  end;
{$M-}

var
  ThreadSMART: TThreadSMART;

implementation

uses
  uFrmSMART, uCommon, uSMART, uTaskFile;

procedure TThreadSMART.InitSMART;
var
  Item: TListItem;
  des, section, section2: string;
  i, sysansi: Integer;
begin
  try
    FrmSMART.IsThreadActive := True;
    if TaskFile.ERR then
      Exit;
    FrmSMART.Caption := TitleFrmSMART + ' - ' + SMART.SmartStatus;
    if not SMART.GetSmart then
      Exit;
    sysansi := GetACP;
    if ((sysansi = 936) or (sysansi = 950)) then
    begin
      if SMART.IsSSD then
      begin
        section := 'SMARTSSDCN';
        section2 := 'SMARTCN';
      end
      else
      begin
        section := 'SMARTCN';
        section2 := 'SMARTSSDCN';
      end;
    end
    else
    begin
      if SMART.IsSSD then
      begin
        section := 'SMARTSSDEN';
        section2 := 'SMARTEN';
      end
      else
      begin
        section := 'SMARTEN';
        section2 := 'SMARTSSDEN';
      end;
    end;
    FrmSMART.lvSMART.Items.Clear;
    FrmSMART.lvSMART.Items.BeginUpdate;
    i := 2;
    while i < 512 do
    begin
      Application.ProcessMessages;
      CopyMemory(SMART.pSmartAttribute, @SMART.bAttribute[i], 12);
      CopyMemory(SMART.pSmartThreshold, @SMART.bThreshold[i], 12);
      if SMART.pSmartAttribute.bAttrID = 0 then
        Break;
      des := MyIniFile.ReadString(section, IntToHex(SMART.pSmartAttribute.bAttrID, 2), '');
      if des = '' then
        des := MyIniFile.ReadString(section2, IntToHex(SMART.pSmartAttribute.bAttrID, 2), '');
      with FrmSMART.lvSMART do
      begin
        Item := Items.Add;
        Item.Caption := Format('%.2X', [SMART.pSmartAttribute.bAttrID]);
        Item.SubItems.Add(des);
        Item.SubItems.Add(Format('%d', [SMART.pSmartAttribute.bAttrValue]));
        Item.SubItems.Add(Format('%d', [SMART.pSmartAttribute.bWorstValue]));
        Item.SubItems.Add(Format('%d', [SMART.pSmartThreshold.bWarrantyThreshold]));
        Item.SubItems.Add(Format('%d', [SMART.pSmartAttribute.bRawValue]));
        Item.SubItems.Add(SMART.StatFlagsToStars(SMART.pSmartAttribute.wStatusFlags));
        Item.SubItems.Add(SMART.StatFlagsToStr(SMART.pSmartAttribute.wStatusFlags));
      end;
      i := i + 12;
    end;
    FrmSMART.lvSMART.Items.EndUpdate;
  finally
    FrmSMART.IsThreadActive := False;
  end;
end;

procedure TThreadSMART.SmartEnalbe;
begin
  try
    FrmSMART.IsThreadActive := True;
    if TaskFile.ERR then
      Exit;
    SMART.SmartEnalbe(True);
  finally
    FrmSMART.IsThreadActive := False;
  end;
end;

procedure TThreadSMART.SmartDisenalbe;
begin
  try
    FrmSMART.IsThreadActive := True;
    if TaskFile.ERR then
      Exit;
    SMART.SmartEnalbe(False);
  finally
    FrmSMART.IsThreadActive := False;
  end;
end;

procedure TThreadSMART.SmartEnableAutoSave;
begin
  try
    FrmSMART.IsThreadActive := True;
    if TaskFile.ERR then
      Exit;
    SMART.SmartEnableAutoSave(True);
  finally
    FrmSMART.IsThreadActive := False;
  end;
end;

procedure TThreadSMART.SmartDisenalbeAutoSave;
begin
  try
    FrmSMART.IsThreadActive := True;
    if TaskFile.ERR then
      Exit;
    SMART.SmartEnableAutoSave(False);
  finally
    FrmSMART.IsThreadActive := False;
  end;
end;

end.

